﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Portal.Logic.Models;
using Portal.Model;
using Portal.Presentation.Models;
using RepositoryFramework.Validators;

namespace Portal.Repository.Bootstraper
{
	public class PresentationAutoMapper : Profile
	{
		protected override void Configure()
		{
			CreateMap<IUserInfo, UserInfoDataModel>()
				.ForMember(x => x.FirstName, opt => opt.MapFrom(src => src.User.FirstName))
				.ForMember(x => x.LastName, opt => opt.MapFrom(src => src.User.LastName))
				.ForMember(x => x.PrimaryEmail, opt => opt.MapFrom(src => src.PrimaryEmail.Value))
				.ForMember(x => x.SecondaryEmail, opt => opt.MapFrom(src => src.SecondaryEmail.Value))
				.ForMember(x => x.PrimaryPhone, opt => opt.MapFrom(src => src.PrimaryPhone.Value))
				.ForMember(x => x.SecondaryPhone, opt => opt.MapFrom(src => src.SecondaryPhone.Value));

			CreateMap<string, IContactInfo>()
				.ConvertUsing(value => new ContactInfo { Value = value });

			CreateMap<UserInfoDataModel, IUser>()
				.IgnoreAllNonExisting();

			Mapper.CreateMap<FluentValidation.Results.ValidationResult, ValidationResult>()
				.ForMember(dest => dest.Errors, opt => opt.MapFrom(m => m.Errors))
				.ForMember(dest => dest.IsValid, opt => opt.MapFrom(m => m.IsValid));

			Mapper.CreateMap<FluentValidation.Results.ValidationFailure, ValidationError>()
				.ForMember(dest => dest.Message, opt => opt.MapFrom(m => m.ErrorMessage));

#if EF
			CreateMap<IUser, User>()
			.ForMember(x => x.Id, opt => opt.Ignore());

			CreateMap<IContactInfo, ContactInfo>()
				.ForMember(x => x.Id, opt => opt.Ignore());
#endif
		
		}
	}
}
